#ifndef __M_UTIL_H__
#define __M_UTIL_H__

#include "logger.hpp"
#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
#include <vector>
#include <memory>
#include <mysql/mysql.h>
#include <jsoncpp/json/json.h>
#include <websocketpp/server.hpp>
#include <websocketpp/config/asio_no_tls.hpp>

typedef websocketpp::server<websocketpp::config::asio> wsserver_t;

class mysql_util{
public:
    static MYSQL *mysql_create(
        const std::string &host,
        const std::string &username,
        const std::string &password,
        const std::string &dbname,
        uint16_t port = 3306){
        // 1.初始化mysql句柄
        MYSQL *mysql = mysql_init(NULL);
        if(mysql == NULL){
            ELOG("mysql init faild!\n");
            return nullptr;
        }
        // 2.连接数据库
        if(mysql_real_connect(mysql, host.c_str(), username.c_str(), password.c_str(), dbname.c_str(), port, NULL, 0) == NULL){
            ELOG("mysql connect faild!\n");
            mysql_close(mysql);
            return nullptr;
        }
        // 3.设置客户端字符集
        if(mysql_set_character_set(mysql, "utf8") != 0){
            ELOG("mysql set character faild!\n");
            mysql_close(mysql);
            return nullptr;
        }
        return mysql;
    }
    static bool mysql_exec(MYSQL *mysql, const std::string &sql){
        int ret = mysql_query(mysql, sql.c_str());
        if(ret != 0){
            ELOG("%s\t", sql.c_str());
            ELOG("mysql query faild! %s\n", mysql_error(mysql));
            // mysql_close(mysql);
            return false;
        }
        return true;
    }
    static void mysql_destroy(MYSQL *mysql){
        if(mysql != NULL){
            mysql_close(mysql);
        }
    }
};

class json_util{
public:
    static bool serialize(const Json::Value &root, std::string &str){
        // 创建工厂类对象StreamWriteBuilder
        Json::StreamWriterBuilder swb;
        // 工厂类对象创建WriteStream对象
        std::unique_ptr<Json::StreamWriter> sw(swb.newStreamWriter());
        // WriteStrream对象把Value对象序列化到stringstream对象中
        std::stringstream ss;
        int n = sw->write(root, &ss);
        if(n != 0){
            ELOG("jsoncpp serialize faild");
            return false;
        }
        str = ss.str();
        return true;
    }
    static bool unserialize(const std::string &str, Json::Value &root){
        // 1. 创建一个CharReaderBuilder工厂类对象
        Json::CharReaderBuilder crb;
        // 2. 使用工厂类对象创建一个CharReader对象
        std::unique_ptr<Json::CharReader> cr(crb.newCharReader());
        // 3. 定义一个Json::Value对象
        std::string err;
        // 4. 使用CharReader对象把stringstream对象中的json字符串反序列化到Value对象中
        bool ret = cr->parse(str.c_str(), str.c_str() + str.size(), &root, &err);
        if(ret == false){
            ELOG("jsoncpp unserialize faild");
            return false;
        }
        return true;
    }
};

class string_util{
public:
    static int split(const std::string &src, const std::string &sep, std::vector<std::string> &dest){
        size_t pos, idx = 0;
        while(idx < src.size()){
            pos = src.find(sep, idx);
            if(pos == std::string::npos){
                dest.push_back(src.substr(idx));
                break;
            }
            if(pos == idx){
                idx = pos + sep.size();
                continue;
            }
            dest.push_back(src.substr(idx, pos - idx));
            idx = pos + sep.size();
        }
        return dest.size();
    }
};

class file_util{
public:
    static bool read(const std::string &filename, std::string &body){
        // 1.打开文件
        std::ifstream ifs(filename, std::ios::binary);
        if(ifs.is_open() == false){
            ELOG("%s file open failed!", filename.c_str());
            return false;
        }
        // 2.获取文件大小
        size_t fsize = 0;
        ifs.seekg(0, std::ios::end);/*设置光标到文件末尾*/
        fsize = ifs.tellg();/*计算光标相对于起始位置的偏移量*/
        ifs.seekg(0, std::ios::beg);/*设置光标到文件起始*/
        body.resize(fsize);
        // 3.读取文件内容
        ifs.read(&body[0], fsize);
        if(ifs.good() == false){
            ELOG("read file %s content failed!", filename.c_str());
            ifs.close();
            return false;
        }
        // 4.关闭文件
        ifs.close();
        return true;
    }
};

#endif
